/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhaojiu.service.impl;

import me.zhengjie.modules.zhaojiu.domain.Period;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhaojiu.repository.PeriodRepository;
import me.zhengjie.modules.zhaojiu.service.PeriodService;
import me.zhengjie.modules.zhaojiu.service.dto.PeriodDto;
import me.zhengjie.modules.zhaojiu.service.dto.PeriodQueryCriteria;
import me.zhengjie.modules.zhaojiu.service.mapstruct.PeriodMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2021-12-09
**/
@Service
@RequiredArgsConstructor
public class PeriodServiceImpl implements PeriodService {

    private final PeriodRepository periodRepository;
    private final PeriodMapper periodMapper;

    @Override
    public Map<String,Object> queryAll(PeriodQueryCriteria criteria, Pageable pageable){
        Page<Period> page = periodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(periodMapper::toDto));
    }

    @Override
    public List<PeriodDto> queryAll(PeriodQueryCriteria criteria){
        return periodMapper.toDto(periodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PeriodDto findById(Long periodId) {
        Period period = periodRepository.findById(periodId).orElseGet(Period::new);
        ValidationUtil.isNull(period.getPeriodId(),"Period","periodId",periodId);
        return periodMapper.toDto(period);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PeriodDto create(Period resources) {
        return periodMapper.toDto(periodRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Period resources) {
        Period period = periodRepository.findById(resources.getPeriodId()).orElseGet(Period::new);
        ValidationUtil.isNull( period.getPeriodId(),"Period","id",resources.getPeriodId());
        period.copy(resources);
        periodRepository.save(period);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long periodId : ids) {
            periodRepository.deleteById(periodId);
        }
    }

    @Override
    public void download(List<PeriodDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PeriodDto period : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", period.getName());
            map.put("培训内容", period.getContent());
            map.put("开课时间", period.getStartDate());
            map.put("结课时间", period.getEndDate());
            map.put("总学时", period.getTotalHours());
            map.put("备注", period.getRemark());
            map.put("状态", period.getEnabled());
            map.put("创建者", period.getCreateBy());
            map.put("更新者", period.getUpdateBy());
            map.put("创建日期", period.getCreateTime());
            map.put("更新时间", period.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
